
<template>
    <div class="model-form">
        <el-page-header @back="goBack" content="更新商品">
        </el-page-header>
        <div class="model-content">
            <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
                <el-row :gutter="10">
    <el-col :span="12">
            <el-form-item label="商品编码"  prop="barCode">
        <el-input v-model="form.barCode" name="barCode">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="商家"  prop="store">
        <el-select style="width: 100%;" v-model="form.store" filterable clearable
                   placeholder="请选择">
            <el-option v-for="item in storeOptions" :key="item.id"
                       :label="item.name" :value="item.id">
            </el-option>
        </el-select>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="商品名称"  prop="name">
        <el-input v-model="form.name" name="name">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="商品主图"  prop="logo">
        <el-input v-model="form.logo" name="logo">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="商品缩略图"  prop="thumbnail">
        <el-input v-model="form.thumbnail" name="thumbnail">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="商品分类"  prop="productCatalog">
        <el-select style="width: 100%;" v-model="form.productCatalog" filterable clearable
                   placeholder="请选择">
            <el-option v-for="item in productCatalogOptions" :key="item.id"
                       :label="item.name" :value="item.id">
            </el-option>
        </el-select>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="库存"  prop="stockNum">
        <el-input v-model="form.stockNum" name="stockNum">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="即时库存"  prop="realStock">
        <el-input v-model="form.realStock" name="realStock">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="价格"  prop="price">
        <el-input v-model="form.price" name="price">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="是否开启规格"  prop="skuEnable">
        <el-input v-model="form.skuEnable" name="skuEnable">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="是否必点"  prop="required">
        <el-input v-model="form.required" name="required">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="折扣"  prop="discount">
        <el-input v-model="form.discount" name="discount">
        </el-input>
            </el-form-item>

    </el-col>
    <el-col :span="12">
            <el-form-item label="商品简介"  prop="summary">
        <el-input v-model="form.summary" name="summary" type="textarea"></el-input>
            </el-form-item>

    </el-col>

                    <el-col :span="22">
                        <el-form-item>
                            <el-button @click="goBack">取消</el-button>
                            <el-button type="primary" @click="updateData">确定</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </div>
</template>

<script>
    import common from "@/mixins/common.js";


    export default {
        name: "product_update",
        mixins: [common],
        components: {
            
        },
        data() {
            return {
                form: {
                    barCode: '',
                    store: '',
                    name: '',
                    logo: '',
                    thumbnail: '',
                    productCatalog: '',
                    stockNum: '',
                    realStock: '',
                    price: '',
                    skuEnable: '',
                    required: '',
                    discount: '',
                    summary: ''                },
                storeOptions: [],
                productCatalogOptions: [],
                rules: {
                    name: [
                        {required: true, message: '请输入商品名称', trigger: 'blur'}
                    ],
                }
            }
        },
        async mounted() {
            let id = this.$route.query.id;
            let data = {};
            data.id = id;
            let res = await this.$http.form("/product/view", data);
            if (res.code === 200) {
                this.form = res.data;
            }
            this.loadStoreOptions();
            this.loadProductCatalogOptions();
        },
        methods: {
            async updateData() {
                try {
                    let valid = await this.$refs["ruleForm"].validate();
                    if (!valid) {
                        return;
                    }
                } catch (e) {
                    return;
                }
                await this.updateDataPost();
            },
            async updateDataPost() {
                let res = await this.$http.form("/product/update", this.form);
                if (res.code !== 200) {
                    this.$message.error(res.msg);
                    return
                }
                this.$message({
                    message: '更新数据成功',
                    type: 'success'
                });
                this.$router.go(-1);
            },
            async loadStoreOptions() {
                let self = this;
                let param = {};
                param.sortMethod = "asc";
                param.sortField = "id";
                param.level = 1;
                param.size = 500;
                param.fetch = 0;
                let res = await this.$http.form("/store/list", param);
                if (res.code === 200) {
                    self.storeOptions = res.data;
                }
            },
            async loadProductCatalogOptions() {
                let self = this;
                let param = {};
                param.sortMethod = "asc";
                param.sortField = "id";
                param.level = 1;
                param.size = 500;
                param.fetch = 0;
                let res = await this.$http.form("/productCatalog/list", param);
                if (res.code === 200) {
                    self.productCatalogOptions = res.data;
                }
            },

        }
    }


</script>

<style scoped>

</style>